var industry,sqrSex,proposerGarden;
var isUploaded = false; // 判断是否已经上传文件
var attentmentPath,timeValues, curLen;
var clientWidth = document.body.clientWidth;
$(function(){
    var date = changeDateFormatArt(new Date());
    if($('#hiddenId').length > 0){
        if(IsPC()){
            $('.form_datetime').val(date);
            $('.pc-time').show();
            $('.phone-time').hide();
            // 点击事件框触发事件
            $('.form_datetime').datetimepicker({
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                language: 'zh-CN',
                format: 'yyyy-mm-dd',
                autoclose: true,
　　              todayHighlight: true,
                todayBtn:  true
            });
        } else {
            $('#time-format').val(date);
            $('.pc-time').hide();
            $('.phone-time').show();
            $("#time-format").datetimePicker({
                title: '时间选择',
                yearSplit: '-',
                monthSplit: '-',
                times: function () {
                },
                onChange: function (picker, values, displayValues) {
                    timeValues = values.join('-')
                }
            });
        }
    }
    if(clientWidth <= 425){
        $('.banner img').attr('src',$().AbsUrlParse("/static/page/images/back3.png"));
        $('.banner img').attr('height','152');
    } else {
        $('.banner img').attr('src',$().AbsUrlParse("/static/page/images/back1.png"));
        $('.banner img').attr('height','400');
    }
    var declarationMobile = sessionStorage.getItem("declarationMobile");
    if(declarationMobile != "" && declarationMobile != undefined){
        $('#loginPhone').html(declarationMobile);
        $('#clickLogin').html('注销');
    } else {
        $('#clickLogin').html('登录');
    }
    var whetherRegister = $('#whether-register option:selected') .val();
    if(whetherRegister == "是"){
        $('.no-register input').attr('disabled',false);
    } else {
        $('.no-register input').attr('disabled',true);
    }
    sqrSex = $("#sex input[name='sex']:checked").val();
    var lengthColor = $("#involved-industry").find('.fbc_optionSvg');
    $("#involved-industry .fbc_optionSvg").each(function () {
        if($(this).css('color') == 'rgb(45, 143, 217)'){
            industry = $(this).parent().find('input').val();
            return false;
        }
    })
})
function show_sub(value){
    if(value == "是"){
        $('.no-register').slideDown(500);
    } else {
        $('.no-register').slideUp(500);
    }
}
function show(value){
    proposerGarden = value;
}
$(document).on("click", "#involved-industry input", function () {
    industry = $(this).val();
    $("#involved-industry input[value=" + industry + "]").parent().find('.fbc_optionSvg').css('color','#2D8FD9');
    $("#involved-industry input[value!=" + industry + "]").parent().find('.fbc_optionSvg').css('color','#C5C5C6');
    if(industry == '其他'){
        $('#showOther').slideDown(500);
    } else {
        $('#other').val('');
        $('#showOther').slideUp(500);
    }
})
$(document).on("click", "#sex input", function () {
    sqrSex = $(this).val();
    $("#sex input[value=" + sqrSex + "]").parent().find('.fbc_optionSvg').css('color','#2D8FD9');
    $("#sex input[value!=" + sqrSex + "]").parent().find('.fbc_optionSvg').css('color','#C5C5C6')
})
$(function(){
    var fileEle = $("#file");
    var maketip = $("#maketip");
    var submitFile = $('#submitFile');
    var uploadtip = $('#uploadtip');
    fileEle.change(function () {
        var fileurl = fileEle.val();
        var fileType = fileurl.substring(fileurl.lastIndexOf('.') + 1);
        var fileName = fileurl.substring(fileurl.lastIndexOf('\\') + 1);
        var files = document.getElementById("file").files;

        if(files.length > 5){
            Common.TipTop($("#submit"), '文件最多可以上传5个文件', "-100px");
            return false;
        }
        fileType = fileType.toLowerCase();
        // 格式是否正确
        if (fileType != "png" && fileType != "jpg" && fileType != "jpeg" && fileType != "doc" && fileType != "docx" && fileType != "txt" && fileType != "rtf" && fileType != "xls" && fileType != "xlsx" && fileType != "ppt" && fileType != "pptx" && fileType != "pdf") {
            Common.TipTop($("#submitFile"), '您上传的文件类型错误，应该上传png、jpg、jpeg、doc，docx，txt，rtf、xls、xlsx、ppt、pptx、pdf文件格式！', "10px");
            $('#file').val('');
            return false;
        }
        if (fileType == "") {
            return false;
        }
        var token = '';
        var sign = MD5('content=' + '');
        // submitFile.hide();
        uploadtip.empty().text(fileName + "上传中...");
        uploadtip.fadeIn(500);
        var uploadOption = {
            url: hostFile +　"fileUpload/file/uploadVoice",
            success: function (res) {
                uploadStatus = res.res.code;
                if (uploadStatus == 40000) {
                    uploadtip.empty().text("上传成功");
                    uploadtip.delay(1000).fadeOut(500);
                    isUploaded = 2;
                    var path = res.res.data[0].path;
                    var  fileurl = $('#file').val();
                    var fileName = fileurl.substring(fileurl.lastIndexOf('\\') + 1);
                    var sameDom = $('#maketip .uploadInfo');
                    for (var i = 0; i < sameDom.length; i++) {
                        if($('#maketip .uploadInfo').eq(i).find('font').html().indexOf(fileName) >= 0){
                            uploadtip.empty().text("");
                            Common.TipTop($("#submit"), "此文件已上传", "-100px");
                            return false;
                        }
                    }
                    $('#maketip').show();
                    var filePath = [];
                    var span = '<div style="border: 1px dashed #d3d3d3;padding: 5px;margin-top:5px;margin-bottom:10px;"><span class="uploadInfo" url='+ res.res.data[0].path +' style="word-wrap:break-word;"><font>' + fileName + '</font></span><div class="deleteFile" style="cursor: pointer;">取消上传</div></div>';
                    maketip.append(span);
                    var dom = $('#maketip .uploadInfo');
                    for (var i = 0; i < dom.length; i++) {
                        filePath[i] = $('#maketip .uploadInfo').eq(i).attr('url');
                    }
                    attentmentPath = filePath.join(',');
                    $('#file').val('');
                    return false;
                } else {
                    $('#maketip').show();
                    maketip.empty().text("上传失败，请重新上传");
                    maketip.delay(1000).fadeOut(500);
                    submitFile.show();
                    return false;
                }
                return false;
            },
            error: function(res) {
                Common.TipTop($("#submit"), res.res.msg, "-100px");
                return false;
            }
        }
        $("#form1").ajaxSubmit(uploadOption);
        return false;
    })
})
$(document).on("click", ".deleteFile", function (e) {
    if($('#edit').html() != "编辑"){
        e.preventDefault();
        isUploaded = false;
        $(this).parent().remove();
        var delFilePath = [];
        $('#file').val('');
        var delDom = $('#maketip .uploadInfo');
        for (var i = 0; i < delDom.length; i++) {
            delFilePath[i] = $('#maketip .uploadInfo').eq(i).attr('url');
        }
        attentmentPath = delFilePath.join(',');
    } else {
        Common.TipTop($("#submit"), "先编辑才能取消", "-100px");
        return false;
    }
})
// 提交
$(document).on("click", "#submit", function () {
    // 获取申请人
    var sqrName = $('#principal').val();
    if(sqrName == ''){
        Common.TipTop($("#submit"), "申请人不能为空", "-100px");
        return false;
    }
    // 手机号码
    var sqrMobile = $('#proposer-phone').val();
    if (!IsMobile(sqrMobile)) {
        Common.TipTop($("#submit"), "申请人手机格式输入有误", "-100px");
        return false;
    }
    if(sqrMobile == ''){
        Common.TipTop($("#submit"), "申报人手机号不能为空", "-100px");
        return false;
    }
    // 邮箱
    var sqrEmail = $('#proposer-email').val();
    if (!IsEmail(sqrEmail)) {
        Common.TipTop($("#submit"), "邮箱格式输入有误", "-100px");
        return false;
    }
    if(sqrEmail == ''){
        Common.TipTop($("#submit"), "申请人邮箱不能为空", "-100px");
        return false;
    }
    // 项目名称
    var projectName = $('#project-name').val();
    if(projectName == ''){
        Common.TipTop($("#submit"), "申请项目名称不能为空", "-100px");
        return false;
    }
    // 是否注册
    var isEntre = $('#whether-register option:selected') .val();
    if(isEntre == "是"){
        var entreName = $('#location-name').val();
        if(entreName == ''){
            Common.TipTop($("#submit"), "所在企业名称不能为空", "-100px");
            return false;
        }
        var regDate;
        if(timeValues != '' || timeValues == null){
            regDate = timeValues;
        }
        if(timeValues == undefined){
           if(changeDateFormatArt(new Date()) == $('#location-time').val()) {
                regDate = changeDateFormatArt(new Date());
            } else {
                if(IsPC()){
                    regDate = $('#location-time').val();
                } else {
                    regDate = changeDateFormatArt(new Date());
                }

            }
        }
        if(regDate == ''){
            Common.TipTop($("#submit"), "企业注册时间不能为空", "-100px");
            return false;
        }
        var regAddress = $('#location-place').val();
        if(regAddress == ''){
            Common.TipTop($("#submit"), "企业注册地不能为空", "-100px");
            return false;
        }
    }
    var actualAddress = $('#location-reality-place').val();
    if(actualAddress == ''){
        Common.TipTop($("#submit"), "实际经营地不能为空", "-100px");
        return false;
    }
    var checkVariable = true;
    if(industry == '其他'){
        if($('#other').val() == ""){
             Common.TipTop($("#submit"), "选其他时请输入行业信息", "-100px");
             return false;
        } else {
            $("#involved-industry input").each(function () {
                if($(this).val() == $('#other').val()){
                    Common.TipTop($("#submit"), "不能与给定行业相同", "-100px");
                    checkVariable = false;
                    return false;
                } else {
                    checkVariable = true;
                }
            })
        }
    }
    var industryTwo = $('#other').val();
    var industryNew;
    if(industry == "其他"){
       industryNew = industryTwo;
    } else{
       industryNew = industry;
    }
    var mainUnit = $('#proposer-garden option:selected') .val();
    var mainUnitId = $('#proposer-garden option:selected') .attr('name');
    var parkUnit = $('#proposer-garden option:selected') .attr('park');
    // 申报联系人
    var sbName = $('#declare-contact').val();
    if(sbName == ""){
        Common.TipTop($("#submit"), "申报联系人不能为空", "-100px");
        return false;
    }
    // 申报人联系手机
    var sbMobile = $('#declare-contact-phone').val();
    if (!IsMobile(sbMobile)) {
        Common.TipTop($("#submit"), "申报联系人手机格式输入有误", "-100px");
        return false;
    }
    if(sbMobile == ""){
        Common.TipTop($("#submit"), "申报人联系手机不能为空", "-100px");
        return false;
    }
    // 手机验证码
    if($(this).html() == "提交"){
        var vCode = $('#phone-code').val();
        if(vCode == ''){
            Common.TipTop($("#submit"), "手机验证码不能为空", "-100px");
            return false;
        }
    }
    // 申报项目简介
    var projectMemo = $('#declare-content').val();
    if(projectMemo == ""){
        Common.TipTop($("#submit"), "申报项目简介不能为空", "-100px");
        return false;
    }
    if(curLen <= 100){
        Common.TipTop($("#submit"), "申报项目简介需要在100字以上", "-100px");
        return false;
    }
    // 获得债权投资、天使投资、股权投资等市场化资金投入情况
    var fundInput = $('#declare-case').val();
    if(fundInput == ""){
        Common.TipTop($("#submit"), "获得债权投资不能为空", "-100px");
        return false;
    }
    var addFilePath = [];
    var addDom = $('#maketip .uploadInfo');
    for (var i = 0; i < addDom.length; i++) {
        addFilePath[i] = $('#maketip .uploadInfo').eq(i).attr('url');
    }
    attentmentPath = addFilePath.join(',');
    if(attentmentPath == "" || attentmentPath == null || attentmentPath == undefined){
        Common.TipTop($("#submit"), "上传文件不能为空", "-100px");
        return false;
    }
    if($('#checkbox-id').is(':checked') == false){
        Common.TipTop($("#submit"), "请先选中大赛免责条款", "-100px");
        return false;
    }
    var videoPath = $('#bd-link').val();
    if(videoPath == '' || videoPath == undefined || videoPath == null){
        videoPath = '';
    } else {
        var str = videoPath.match(/http:\/\/pan\.baidu\.com.+/);
        if(str == null || str == ""){
            var strTwo = videoPath.match(/https:\/\/pan\.baidu\.com.+/);
            if(strTwo == null || strTwo == ""){
                Common.TipTop($("#submit"), "您输入的百度网盘链接格式不对", "-100px");
                return false;
            }
        }
    }
    if(checkVariable){
    if($(this).html() == "提交"){
        if(isEntre == "是"){
            var data = {
              'sqrName': sqrName,
              'sqrSex': sqrSex,
              'sbMobile': sqrMobile,
              'vCode': vCode,
              'sqrEmail': sqrEmail,
              'projectName': projectName,
              'isEntre': isEntre,
              'entreName': entreName,
              'regDate': regDate,
              'regAddress': regAddress,
              'actualAddress': actualAddress,
              'industry': industryNew,
              'mainUnit': mainUnit,
              'sbName': sbName,
              'sqrMobile': sbMobile,
              'projectMemo': projectMemo,
              'fundInput': fundInput,
              'attentmentPath': attentmentPath,
              'parkId': mainUnitId,
              'mainUnit': parkUnit,
              'videoPath': videoPath
          }
        } else {
            var data = {
              'sqrName': sqrName,
              'sqrSex': sqrSex,
              'sbMobile': sqrMobile,
              'vCode': vCode,
              'sqrEmail': sqrEmail,
              'projectName': projectName,
              'isEntre': isEntre,
              'actualAddress': actualAddress,
              'industry': industryNew,
              'sbName': sbName,
              'sqrMobile': sbMobile,
              'projectMemo': projectMemo,
              'fundInput': fundInput,
              'attentmentPath': attentmentPath,
              'parkId': mainUnitId,
              'mainUnit': parkUnit,
              'videoPath': videoPath
            }
        }
        getdata(host + "userproject/saveDeclareProject", data,
            function(res){
                if( res.res.code == 40000){
                    Common.TipTop($("#submit"), '提交成功', "-100px");
                    sessionStorage.setItem("kid", res.res.data);
                    $('input').val('');
                    $('textarea').val('');
                    isUploaded = false;
                    $("#maketip").empty();
                    $("#file").val("");
                    $('#submitFile').show();
                    window.location.href = $().AbsUrlParse("/web/page/share?kid=" + res.res.data);
                    // window.location.href = "share.html";
                    return false;
                } else {
                    Common.TipTop($("#submit"), res.res.msg, "-100px");
                    return false;
                }
            },
            function(res){
                $('#whether-repeat').modal('show');
            }
        );
    } else {
        var kid = sessionStorage.getItem("kid");
        if(isEntre == "是"){
            var data = {
              'kid': kid,
              'sqrName': sqrName,
              'sqrSex': sqrSex,
              'sbMobile': sqrMobile,
              'sqrEmail': sqrEmail,
              'projectName': projectName,
              'isEntre': isEntre,
              'entreName': entreName,
              'regDate': regDate,
              'regAddress': regAddress,
              'actualAddress': actualAddress,
              'industry': industryNew,
              'mainUnit': mainUnit,
              'sbName': sbName,
              'sqrMobile': sbMobile,
              'projectMemo': projectMemo,
              'fundInput': fundInput,
              'attentmentPath': attentmentPath,
              'parkId': mainUnitId,
              'mainUnit': parkUnit,
              'videoPath': videoPath
            }
        } else {
            var data = {
              'kid': kid,
              'sqrName': sqrName,
              'sqrSex': sqrSex,
              'sbMobile': sqrMobile,
              'sqrEmail': sqrEmail,
              'projectName': projectName,
              'isEntre': isEntre,
              'actualAddress': actualAddress,
              'industry': industryNew,
              'sbName': sbName,
              'sqrMobile': sbMobile,
              'projectMemo': projectMemo,
              'fundInput': fundInput,
              'attentmentPath': attentmentPath,
              'parkId': mainUnitId,
              'mainUnit': parkUnit,
              'videoPath': videoPath
            }
        }
        getdata(host + "userproject/updateDeclareProject", data,
            function(res){
               if(res.res.code == 40000){
                    updateData(1);
               } else {
                    Common.TipTop($("#submit"), res.res.msg, "-100px");
                    return false;
               }
            }
        )
    }
}
 })
$(document).on("click", "#declare-content", function () {
    $paytextarea = $(".paytextarea");
    wordLength($paytextarea);
})

function wordLength($text) {
    function getTxtLen(obj) {
        return $(obj)[0].value.replace(/[^\x00-\xff]/g, 'xx').length;
    }
    var curTxt = "";
    var textarea = null;
    textarea = $text[0];
    var checkTxtLen = function() {
        if (textarea && curTxt != textarea.value) {
            curTxt = textarea.value;
            var normal = "您还可以输入<em class=\"pipsLim\">0</em>字";
            var error = "已超出<em class=\"pipsLim\">1</em>字";
            curLen = getTxtLen($text);
            // if(curLen > 100){
            //     Common.TipTop($(".declare-content-box"), '超过100字', "-100px");
            // }
            return false;
        }
    };
    $text.on("keyup focus blur", function(e) {
        checkTxtLen();
    });
}

// 点击登录
$(document).on("click", "#clickLogin", function () {
    if($(this).html() == "登录"){
        window.location.href = $().AbsUrlParse("/web/page/login");
        // window.location.href = "login.html";
    } else {
        sessionStorage.removeItem("declarationMobile");
        sessionStorage.removeItem("kid");
        sessionStorage.removeItem("data");
        sessionStorage.removeItem("loginKey");
        $(this).html('登录');
        $('#loginPhone').html('');
        window.location.href = $().AbsUrlParse("/web/page/login");
    }
})
